package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"github.com/HarsonYoung/nat-type-test/tester"
	"log"
)

func creatWindow() {
	myApp := app.New()
	myWindow := myApp.NewWindow("NatTypeTest")
	selectList := []string{"stun.qq.com", "stun.miwifi.com"}

	serverLabel := widget.NewLabel("STUN Server:")
	serverSelect := widget.NewSelectEntry(selectList)
	serverSelect.Text = "stun.qq.com"
	blockA := container.New(layout.NewVBoxLayout(), serverLabel, layout.NewSpacer(), serverSelect)

	natType := widget.NewLabel("NAT Type:")
	natTypeStr := binding.NewString()
	natTypeEntry := widget.NewEntryWithData(natTypeStr)
	//NatTypeEntry.Disable()
	blockB := container.New(layout.NewVBoxLayout(), natType, layout.NewSpacer(), natTypeEntry)

	localEnd := widget.NewLabel("Local End:")
	localEndStr := binding.NewString()
	localEndEntry := widget.NewEntryWithData(localEndStr)
	//LocalEndEntry.Disable()
	blockC := container.New(layout.NewVBoxLayout(), localEnd, layout.NewSpacer(), localEndEntry)

	publicEnd := widget.NewLabel("Public End:")
	publicEndStr := binding.NewString()
	publicEndEntry := widget.NewEntryWithData(publicEndStr)
	//PublicEndEntry.Disable()
	blockD := container.New(layout.NewVBoxLayout(), publicEnd, layout.NewSpacer(), publicEndEntry)

	testButton := widget.NewButton("Test", func() {
		natType, respVal := tester.GetNatType(serverSelect.Text, 3478)
		log.Println("RespVal->", respVal)
		natTypeStr.Set(natType)
		localAddr := "0.0.0.0:54868"
		localEndStr.Set(localAddr)
		externalAddr := respVal["ExternalIP"] + ":" + respVal["ExternalPort"]
		publicEndStr.Set(externalAddr)
	})

	centered := container.New(layout.NewVBoxLayout(), blockA, blockB, blockC, blockD, testButton)
	myWindow.SetContent(centered)
	myWindow.Resize(fyne.NewSize(550, 500))
	myWindow.ShowAndRun()
}

func main() {
	creatWindow()
}
